package com.bytecollege.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bytecollege.mapper.*;
import com.bytecollege.model.*;
import com.bytecollege.service.AdminService;
import com.bytecollege.vo.AdminArticleVO;
import com.bytecollege.vo.AdminCourseVO;
import com.bytecollege.vo.AdminUserVO;
import com.bytecollege.vo.ArticleDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: AdminService
 * @Description: 管理员服务层实现类
 * @Author: Sun
 * @Date: 2022/10/19 17:02
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CtypeMapper ctypeMapper;
    @Autowired
    private NoticeMapper noticeMapper;


    /**
    * @Description: 根据传入条件查找管理员
    * @Param: [queryWrapper]
    * @return: com.bytecollege.model.Admin
    * @Author: xigua
    * @Date: 2022/10/19
    */
    @Override
    public Admin getOne(Wrapper<Admin> queryWrapper) {
        return adminMapper.selectOne(queryWrapper);
    }

    /***
     * @Description: 查找所有的待审核文章
     * @Param: [page]
     * @return: com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.bytecollege.model.Article>
     * @Author: xigua
     * @Date: 2022/10/24
     */
    public Page<AdminArticleVO> getAllToBeCheck(Page<AdminArticleVO> page){
        return articleMapper.getAllByCheck(page);
    }

    /***
     * @Description: 管理员审核文章通过
     * @Param: []
     * @return: int
     * @Author: xigua
     * @Date: 2022/10/24
     */
    public int checkSuccess(Integer articleid){
        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<Article>().set("article_state", 1).eq("article_id", articleid);
        return articleMapper.update(articleMapper.selectById(articleid),updateWrapper);
    }

    /***
    * @Description: 审核不通过
    * @Param: [articleid]
    * @return: int
    * @Author: xigua
    * @Date: 2022/10/25
    */
    public int checkFail(Integer articleid){
        QueryWrapper<Article> queryWrapper = new QueryWrapper<Article>().eq("article_id", articleid);
        return articleMapper.delete(queryWrapper);
    }

    /***
     * @Description: 根据关键字key搜索用户
     * @Param: [key]
     * @return: java.util.List<com.bytecollege.model.User>
     * @Author: xigua
     * @Date: 2022/10/20
     */
    public List<User> getUserBySearch(String key){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("User_Name",key).or().like("User_NickName",key)
                .or().likeLeft("User_Gender",key);
        List<User> userBySearch = userMapper.selectList(wrapper);
        userBySearch.forEach(System.out::println);
        return userBySearch;
    }

    /***
     * @Description: 通过Id查找用户，设置isDelete为1（封禁）
     * @Param: [userid]
     * @return: int
     * @Author: xigua
     * @Date: 2022/10/20
     */
    public int banUser(Integer userid){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>().set("User_isDelete", 1).eq("User_Id", userid);
        return userMapper.update(userMapper.selectById(userid),updateWrapper);
    }

    /***
     * @Description: 通过Id查找用户，设置isDelete为0（取消封禁）
     * @Param: [userid]
     * @return: int
     * @Author: xigua
     * @Date: 2022/10/20
     */
    public int cancelBanUser(Integer userid){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>().set("User_isDelete", 0).eq("User_Id", userid);
        return userMapper.update(userMapper.selectById(userid),updateWrapper);
    }

    /***
     * @Description: 管理员修改用户
     * @Param: [adminUserVo]
     * @return: int
     * @Author: xigua
     * @Date: 2022/10/21
     */
    public int adminModUser(AdminUserVO adminUserVo){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>().set("User_Name", adminUserVo.getUsername())
                .set("User_Pwd", adminUserVo.getUserpassword())
                .set("User_NickName", adminUserVo.getUsernickname())
                .set("User_Email", adminUserVo.getUseremail())
                .set("User_Phone", adminUserVo.getUserphone())
                .eq("User_Id", adminUserVo.getUserid());
        return userMapper.update(userMapper.selectById(adminUserVo.getUserid()),updateWrapper);
    }

    /***
    * @Description: 管理员查询课程
    * @Param: [page]
    * @return: com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.bytecollege.vo.AdminCourseVO>
    * @Author: xigua
    * @Date: 2022/10/25
    */
    public Page<AdminCourseVO> getAllCourse(Page<AdminCourseVO> page){
        return adminMapper.getCourseByAdmin(page);
    }

    /***
    * @Description: 管理员删除课程
    * @Param: [courseid]
    * @return: int
    * @Author: xigua
    * @Date: 2022/10/25
    */
    public int deleteCourse(Integer courseid){
        QueryWrapper<Course> queryWrapper = new QueryWrapper<Course>().eq("course_id", courseid);
        return courseMapper.delete(queryWrapper);
    }

    /***
    * @Description: 查找所有课程类型
    * @Param: []
    * @return: java.util.List<com.bytecollege.model.Ctype>
    * @Author: xigua
    * @Date: 2022/10/25
    */
    public List<Ctype> getAllCtype(){
        return ctypeMapper.selectList(null);
    }

    /***
    * @Description: 修改课程图片
    * @Param: [courseid, coursepic]
    * @return: int
    * @Author: xigua
    * @Date: 2022/10/25
    */
    public int modCoursePic(Integer courseid,String coursepic){
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<Course>().set("course_pic", coursepic).eq("course_id", courseid);
        return courseMapper.update(courseMapper.selectById(courseid),updateWrapper);
    }

    /***
    * @Description: 管理员添加课程
    * @Param: [adminCourseVO]
    * @return: int
    * @Author: xigua
    * @Date: 2022/10/25
    */
    public int addCourse(AdminCourseVO adminCourseVO){
        Course course= Course.builder()
                .courseName(adminCourseVO.getCourseName())
                .courseDescribe(adminCourseVO.getCourseDescribe())
                .coursePic(adminCourseVO.getCoursePic())
                .courseStatus(adminCourseVO.getCourseStatus())
                .courseUrl(adminCourseVO.getCourseUrl())
                .ctypeId(adminCourseVO.getCtypeId())
                .build();
        return courseMapper.insert(course);
    }

    /**
     * 管理员查看还未通过审核的文章
     * @param articleId
     * @return
     */
    public ArticleDetailVo findArticleById(Integer articleId){
        QueryWrapper<ArticleDetailVo> articleDetailVoQueryWrapper = new QueryWrapper<>();
        articleDetailVoQueryWrapper.eq("ar.article_id",articleId);
        articleDetailVoQueryWrapper.eq("ar.article_state",0);
        return articleMapper.getArticleById(articleDetailVoQueryWrapper);
    }

    /***
    * @Description: 更新课程信息
    * @Param: [adminCourseVO]
    * @return: int
    * @Author: xigua
    * @Date: 2022/10/26
    */
    public int updateCourse(AdminCourseVO adminCourseVO){
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<Course>().set("course_name", adminCourseVO.getCourseName())
                .set("course_describe", adminCourseVO.getCourseDescribe())
                .set("course_status", adminCourseVO.getCourseStatus())
                .set("course_url", adminCourseVO.getCourseUrl())
                .eq("course_id", adminCourseVO.getCourseId());
        return courseMapper.update(courseMapper.selectById(adminCourseVO.getCourseId()),updateWrapper);
    }
    
    /***
    * @Description: 查询所有已审核通过的文章
    * @Param: [page]
    * @return: com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.bytecollege.model.Article>
    * @Author: xigua
    * @Date: 2022/10/26
    */
    public Page<Article> getAllArticle(Page<Article> page){
        //只查询审核通过的文章
        QueryWrapper<Article> queryWrapper = new QueryWrapper<Article>().eq("article_state", 1);
        return articleMapper.selectPage(page,queryWrapper);
    }

    /**
    * @Description: 管理员删除文章
    * @Param: [article_id]
    * @return: int
    * @Author: xigua
    * @Date: 2022/10/26
    */
    public int delArticle(Integer article_id){
       return articleMapper.delete(new QueryWrapper<Article>().eq("article_id",article_id));
    }
    
    /***
    * @Description: 公告的增删查改
    * @Param: 
    * @return: 
    * @Author: xigua
    * @Date: 2022/10/26
    */
    public int addNotice(Notice notice){
        return noticeMapper.insert(notice);
    }
    public int delNotice(Integer noticeid){
        return noticeMapper.deleteById(noticeid);
    }
    public List<Notice> getAllNotice(){
        QueryWrapper<Notice> wrapper = new QueryWrapper<Notice>().orderByDesc("notice_addtime");
        return noticeMapper.selectList(wrapper);
    }
    public int modNotice(Notice notice){
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<Notice>().set("notice_content", notice.getNoticeContent())
                .set("notice_name", notice.getNoticeName())
                .eq("notice_id", notice.getNoticeId());
        return noticeMapper.update(noticeMapper.selectById(notice.getNoticeId()),updateWrapper);
    }
}
